package com.bocloud.cmp.service.app.monitor;

import java.util.concurrent.Callable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.app.resource.OpenshiftMonitor;
import com.bocloud.common.http.HttpClient;
import com.bocloud.common.model.BaseResult;
/**
 * @author Zaney
 * @data:2017年4月7日
 * @describe:容器平台监控任务类
 */
public class KubernetesMonitorTask implements Callable<BaseResult<JSONObject>> {
	private static Logger logger = LoggerFactory.getLogger(KubernetesMonitorTask.class);
	private OpenshiftMonitor monitor;
	private KubernetesOpenshiftMonitorClient client;

	public KubernetesMonitorTask(OpenshiftMonitor monitor) {
		super();
		this.monitor = monitor;
		client = new KubernetesOpenshiftMonitorClient();
		client.setHttpClient(new HttpClient());
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public BaseResult<JSONObject> call() throws Exception {
		switch (monitor.getResourceType()) {
		case "cpu":
			JSONObject cpuMonitor = client.cpuMonitor(monitor);
			if (null == cpuMonitor) {
				logger.error("get cpu monitor info error");
				return new BaseResult(false, "get cpu monitor info error", null);
			}
			return new BaseResult(true, "success", cpuMonitor);
		case "memory":
			JSONObject memoryMonitor = client.memoryMonitor(monitor);
			if (null == memoryMonitor) {
				logger.error("get memory monitor info error");
				return new BaseResult(false, "get memory monitor info error", null);
			}
			return new BaseResult(true, "success", memoryMonitor);
		case "networkRx":
			JSONObject networkRxMonitor = client.networkRxMonitor(monitor);
			if (null == networkRxMonitor) {
				logger.error("get networkRx monitor info error");
				return new BaseResult(false, "get networkRx monitor info error", null);
			}
			return new BaseResult(true, "success", networkRxMonitor);
		default:
			JSONObject networkTxMonitor = client.networkTxMonitor(monitor);
			if (null == networkTxMonitor) {
				logger.error("get networkTx monitor info error");
				return new BaseResult(false, "get networkTx monitor info error", null);
			}
			return new BaseResult(true, "success", networkTxMonitor);
		}
	}

}
